<!DOCTYPE html>
<html >
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    
    <title>从H264中SPS计算宽高 | xilixili.net</title>
    <meta name="renderer" content="webkit">
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <meta name="description" content="最近在处理H264流的时候发现计算的宽高并不正确。
基本计算方法大家都知道SPS解析之后，宽高就可以通过下面的计算公式计算出来：

宽 (pic_width_in_mbs_minus1 + 1) * 16
高 (pic_height_in_map_units_minus1 + 1) * 16

然而仔细思考一下就发现，这种方式计算的宽高都是16的倍数，但好像和平时的认知不符啊。
其实针对不是16整">

    <meta name="twitter:card" content="summary">
    <meta name="twitter:title" content="从H264中SPS计算宽高 | xilixili.net">
    <meta name="twitter:description" content="最近在处理H264流的时候发现计算的宽高并不正确。
基本计算方法大家都知道SPS解析之后，宽高就可以通过下面的计算公式计算出来：

宽 (pic_width_in_mbs_minus1 + 1) * 16
高 (pic_height_in_map_units_minus1 + 1) * 16

然而仔细思考一下就发现，这种方式计算的宽高都是16的倍数，但好像和平时的认知不符啊。
其实针对不是16整">

    <meta property="og:type" content="article">
    <meta property="og:title" content="从H264中SPS计算宽高 | xilixili.net">
    <meta property="og:description" content="最近在处理H264流的时候发现计算的宽高并不正确。
基本计算方法大家都知道SPS解析之后，宽高就可以通过下面的计算公式计算出来：

宽 (pic_width_in_mbs_minus1 + 1) * 16
高 (pic_height_in_map_units_minus1 + 1) * 16

然而仔细思考一下就发现，这种方式计算的宽高都是16的倍数，但好像和平时的认知不符啊。
其实针对不是16整">

    
    <meta name="author" content="rangerlee">
    
    <link rel="stylesheet" href="/css/vno.css">
    <link rel="stylesheet" href="/css/font-awesome.min.css">

    
    <link rel="icon" href="/images/avatar-small.png">
    

    <meta name="generator" content="hexo"/>
    
    <link rel="alternate" type="application/rss+xml" title="xilixili.net" href="/atom.xml">
    

    <link rel="canonical" href="xilixili.net/2021/02/08/h264-get-width-height-from-sps/"/>

                 
</head>

<body class="home-template no-js">
    <script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script src="/js/main.js"></script>
    <span class="mobile btn-mobile-menu">
        <i class="fa fa-list btn-mobile-menu__icon"></i>
        <i class="fa fa-angle-up btn-mobile-close__icon hidden"></i>
    </span>

    
<header class="panel-cover panel-cover--collapsed">
  <div class="panel-main">
    <div class="panel-main__inner panel-inverted">
    <div class="panel-main__content">

        <h1 class="panel-cover__title panel-title"><a href="/" title="访问首页 xilixili.net">xilixili.net</a></h1>
        
        <span class="panel-cover__subtitle panel-subtitle"><!-- 生活と技术の杂记 --></span>
        
        <hr class="panel-cover__divider">
        
        <p class="panel-cover__description">生活と技术の杂记</p>
        
	<hr class="panel-cover__divider panel-cover__divider--secondary">

        <div class="navigation-wrapper">
          <div>
          <nav class="cover-navigation cover-navigation--primary">
            <ul class="navigation">
              <li class="navigation__item"><a href="/#blog" title="访问博客" class="blog-button">博客</a></li>
            
            </ul>
          </nav>
          </div>
          <div>
          <nav class="cover-navigation navigation--social">
  <ul class="navigation">

  <!-- Weibo-->
  

  <!-- Github -->
  
  <li class="navigation__item">
    <a href="https://github.com/rangerlee" title="查看我的GitHub主页" target="_blank">
      <i class="social fa fa-github"></i>
      <span class="label">Github</span>
    </a>
  </li>


<!-- Stack Overflow -->
        

  <!-- Google Plus -->
  

<!-- Facebook -->

  
<!-- Twitter -->

  

  <li class="navigation__item">
    <a href="/atom.xml" title="RSS" target="_blank">
      <i class="social fa fa-rss"></i>
      <span class="label">RSS</span>
    </a>
  </li>


  <li class="navigation__item">
    <a href="mailto:http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=RzUmKSAiNSsiIgchKD8qJi4raSQoKg" title="邮件联系我" target="_blank">
      <i class="social fa fa-envelope"></i>
      <span class="label">Email</span>
    </a>
  </li>


  </ul>
</nav>

          </div>
        </div>

      </div>

    </div>

    <div class="panel-cover--overlay cover-alpha"></div>
  </div> 
</header>
<script type="text/javascript">
var side = document.getElementsByClassName("panel-cover")[0];
function checkWebp() {
	try{
		return(document.createElement('canvas').toDataURL('image/webp').indexOf('data:image/webp') == 0);
	}catch(err) {
		return false;
	}
};

if(checkWebp()) {
	side.style.backgroundImage = "url('/images/background-cover.jpg.webp')";
} else {
	side.style.backgroundImage = "url('/images/background-cover.jpg')";
}
</script>


    <div class="content-wrapper">
        <div class="content-wrapper__inner">
            <article class="post-container post-container--single">

  <header class="post-header">
    <div class="post-meta">
      <time datetime="2021-02-08T10:54:00.000Z" class="post-list__meta--date date">2021-02-08</time> &#8226; <span class="post-meta__tags tags">
  <a class="tag-link" href="/tags/技术/">技术</a>
 </span>
      <span class="page-pv">
       阅读 <span id="busuanzi_value_page_pv"><i class="fa fa-spinner fa-spin"></i></span>
      </span> 
   
    </div>
    <h1 class="post-title">从H264中SPS计算宽高</h1>
  </header>

  <section class="post">
    <p>最近在处理H264流的时候发现计算的宽高并不正确。</p>
<h3 id="基本计算方法"><a href="#基本计算方法" class="headerlink" title="基本计算方法"></a>基本计算方法</h3><p>大家都知道SPS解析之后，宽高就可以通过下面的计算公式计算出来：</p>
<ul>
<li>宽 <code>(pic_width_in_mbs_minus1 + 1) * 16</code></li>
<li>高 <code>(pic_height_in_map_units_minus1 + 1) * 16</code></li>
</ul>
<p>然而仔细思考一下就发现，这种方式计算的宽高都是16的倍数，但好像和平时的认知不符啊。</p>
<p>其实针对不是16整数倍的值，<code>frame_cropping_flag</code> 值为1，<code>frame_mbs_only_flag</code> 为1，如下</p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line">width = ((sps-&gt;pic_width_in_mbs_minus1 +<span class="number">1</span>)*<span class="number">16</span>) - sps-&gt;frame_crop_left_offset*<span class="number">2</span> </span><br><span class="line">    - sps-&gt;frame_crop_right_offset*<span class="number">2</span>;  </span><br><span class="line">height = ((<span class="number">2</span> - sps-&gt;frame_mbs_only_flag)* (sps-&gt;pic_height_in_map_units_minus1 +<span class="number">1</span>)*<span class="number">16</span>) </span><br><span class="line">    - (sps-&gt;frame_crop_top_offset*<span class="number">2</span>) - (sps-&gt;frame_crop_bottom_offset*<span class="number">2</span>);</span><br></pre></td></tr></table></figure>
<p>这样计算出来的才是正确的。</p>
<h3 id="终极计算方法"><a href="#终极计算方法" class="headerlink" title="终极计算方法"></a>终极计算方法</h3><p>然后上述的计算方法还是有一些局限性，具体参见标准文档，比较靠谱的方法如下</p>
<figure class="highlight cpp"><table><tr><td class="code"><pre><span class="line">width  = (sps-&gt;pic_width_in_mbs_minus1+<span class="number">1</span>) * <span class="number">16</span>;</span><br><span class="line">height = (<span class="number">2</span> - sps-&gt;frame_mbs_only_flag)* (sps-&gt;pic_height_in_map_units_minus1 +<span class="number">1</span>) * <span class="number">16</span>);</span><br><span class="line"><span class="keyword">if</span>(sps-&gt;frame_cropping_flag)&#123;</span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">int</span> crop_unit_x;</span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">int</span> crop_unit_y;</span><br><span class="line">    <span class="keyword">if</span> (<span class="number">0</span> == sps-&gt;chroma_format_idc)&#123;</span><br><span class="line">    	<span class="comment">// monochrome</span></span><br><span class="line">        crop_unit_x = <span class="number">1</span>;</span><br><span class="line">        crop_unit_y = <span class="number">2</span> - sps-&gt;frame_mbs_only_flag;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">1</span> == sps-&gt;chroma_format_idc)&#123; </span><br><span class="line">    	<span class="comment">// 4:2:0</span></span><br><span class="line">        crop_unit_x = <span class="number">2</span>;</span><br><span class="line">        crop_unit_y = <span class="number">2</span> * (<span class="number">2</span> - sps-&gt;frame_mbs_only_flag);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">2</span> == sps-&gt;chroma_format_idc)&#123; </span><br><span class="line">    	<span class="comment">// 4:2:2</span></span><br><span class="line">        crop_unit_x = <span class="number">2</span>;</span><br><span class="line">        crop_unit_y = <span class="number">2</span> - sps-&gt;frame_mbs_only_flag;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123; <span class="comment">// 3 == sps.chroma_format_idc   </span></span><br><span class="line">    	<span class="comment">// 4:4:4</span></span><br><span class="line">        crop_unit_x = <span class="number">1</span>;</span><br><span class="line">        crop_unit_y = <span class="number">2</span> - sps-&gt;frame_mbs_only_flag;</span><br><span class="line">    &#125;</span><br><span class="line">        </span><br><span class="line">    width  -= crop_unit_x * (sps-&gt;frame_crop_left_offset + sps-&gt;frame_crop_right_offset);</span><br><span class="line">    height -= crop_unit_y * (sps-&gt;frame_crop_top_offset  + sps-&gt;frame_crop_bottom_offset);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

  </section>

</article>

<section class="read-more">
           
    
               
            <div class="read-more-item">
                <span class="read-more-item-dim">最近的文章</span>
                <h2 class="post-list__post-title post-title"><a href="/2021/03/25/rtp-out-of-order/" title="视频RTP乱序问题">视频RTP乱序问题</a></h2>
                <p class="excerpt">
                
                问题分析最近在一个新环境下保持RTP的H264视频数据花屏，按照以往经验，最大可能性是丢包了，遂抓包分析，发现包个数足够，并没有反馈任何包的丢失，这就有意思了。
不过在看抓包过程中，不经意间发现了下面的现象

seq 51 的包跑到 seq 50 的前面了，怪不得解包的视频不正确。顺着这个线索继续看
                &hellip;
                </p>
                <div class="post-list__meta"><time datetime="2021-03-25T06:51:00.000Z" class="post-list__meta--date date">2021-03-25</time> &#8226; <span class="post-list__meta--tags tags">
  <a class="tag-link" href="/tags/技术/">技术</a>
</span><a class="btn-border-small" href="/2021/03/25/rtp-out-of-order/">继续阅读</a></div>
                           
            </div>
        
        
               
            <div class="read-more-item">
                <span class="read-more-item-dim">更早的文章</span>
                <h2 class="post-list__post-title post-title"><a href="/2020/12/14/centos-6-config-vault-yum-repo/" title="CentOS6支持到期后配置备份更新源">CentOS6支持到期后配置备份更新源</a></h2>
                <p class="excerpt">
                
                CentOS 6 的支持在上个月到期了，各个更新源都下线了，线上一堆的服务器又不能升级，偶尔安装个软件可怎么办，其实官方还保留了一个备份的源用来更新，虽然不升级了，但至少安装软件没问题。
配置备份虽然说备份已经没啥用了，但是作为良好的操作习惯，还是建议做一下。
备份目录 /etc/yum.repo.
                &hellip;
                </p>
                <div class="post-list__meta"><time datetime="2020-12-14T09:41:00.000Z" class="post-list__meta--date date">2020-12-14</time> &#8226; <span class="post-list__meta--tags tags">
  <a class="tag-link" href="/tags/技术/">技术</a>
</span><a class="btn-border-small" href="/2020/12/14/centos-6-config-vault-yum-repo/">继续阅读</a></div>
                       
            </div>
        
     
   
   
  
</section>



	<script>
	(function(){
		var bp = document.createElement('script');
		var curProtocol = window.location.protocol.split(':')[0];
		if (curProtocol === 'https') {
			bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
		}
		else {
			bp.src = 'http://push.zhanzhang.baidu.com/push.js';
		}
		var s = document.getElementsByTagName("script")[0];
		s.parentNode.insertBefore(bp, s);
	})();
	</script>



<script>
(function(){
var src = (document.location.protocol == "http:") ? "http://js.passport.qihucdn.com/11.0.1.js?8612a7fa26d7ba4562be733ae33649b1":"https://jspassport.ssl.qhimg.com/11.0.1.js?8612a7fa26d7ba4562be733ae33649b1";
document.write('<script src="' + src + '" id="sozz"><\/script>');
})();
</script>



            <footer class="footer">
    <span class="footer__copyright">
        &copy; 2023 rangerlee - 本站点采用 <a href="http://creativecommons.org/licenses/by-nc-sa/4.0/">知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议</a>
       
    </span>
    <span class="footer__copyright">
             - 本站基于开源 <a href="http://hexo.io">Hexo</a> 搭建，采用 <a href="https://github.com/rangerlee/hexo-theme-new-vno">hexo-theme-new-vno</a> 主题，修改自 <a href="https://github.com/monniya/hexo-theme-new-vno ">new-vno</a> 主题  <a href="http://www.beian.miit.gov.cn">皖ICP备16009457号</a>
         </span>
       
    
    
</footer>


        </div>
    </div>

     
    
    <script>
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "//hm.baidu.com/hm.js?bab4f71524319c10819bcae280021624";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
        })();
    </script>



    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    
    
</body>
</html>
